<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>mainwindow.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-mainwindows-mainwindow-example.html">Main Window</a></td><td >mainwindow.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">mainwindow.cpp 示例文件</h1>
<span class="subtitle">mainwindows/mainwindow/mainwindow.cpp</span>
<!-- $$$mainwindows/mainwindow/mainwindow.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the demonstration applications of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>
  <span class="preprocessor">#include &quot;colorswatch.h&quot;</span>
  <span class="preprocessor">#include &quot;toolbar.h&quot;</span>

  <span class="preprocessor">#include &lt;QAction&gt;</span>
  <span class="preprocessor">#include &lt;QLayout&gt;</span>
  <span class="preprocessor">#include &lt;QMenu&gt;</span>
  <span class="preprocessor">#include &lt;QMenuBar&gt;</span>
  <span class="preprocessor">#include &lt;QStatusBar&gt;</span>
  <span class="preprocessor">#include &lt;QTextEdit&gt;</span>
  <span class="preprocessor">#include &lt;QFile&gt;</span>
  <span class="preprocessor">#include &lt;QDataStream&gt;</span>
  <span class="preprocessor">#include &lt;QFileDialog&gt;</span>
  <span class="preprocessor">#include &lt;QDialogButtonBox&gt;</span>
  <span class="preprocessor">#include &lt;QMessageBox&gt;</span>
  <span class="preprocessor">#include &lt;QApplication&gt;</span>
  <span class="preprocessor">#include &lt;QPainter&gt;</span>
  <span class="preprocessor">#include &lt;QMouseEvent&gt;</span>
  <span class="preprocessor">#include &lt;QLineEdit&gt;</span>
  <span class="preprocessor">#include &lt;QComboBox&gt;</span>
  <span class="preprocessor">#include &lt;QLabel&gt;</span>
  <span class="preprocessor">#include &lt;QPushButton&gt;</span>
  <span class="preprocessor">#include &lt;QTextEdit&gt;</span>
  <span class="preprocessor">#include &lt;QDebug&gt;</span>

  <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> message<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span>
      <span class="string">&quot;&lt;p&gt;&lt;b&gt;Qt Main Window Example&lt;/b&gt;&lt;/p&gt;&quot;</span>

      <span class="string">&quot;&lt;p&gt;This is a demonstration of the QMainWindow, QToolBar and &quot;</span>
      <span class="string">&quot;QDockWidget classes.&lt;/p&gt;&quot;</span>

      <span class="string">&quot;&lt;p&gt;The tool bar and dock widgets can be dragged around and rearranged &quot;</span>
      <span class="string">&quot;using the mouse or via the menu.&lt;/p&gt;&quot;</span>

      <span class="string">&quot;&lt;p&gt;Each dock widget contains a colored frame and a context &quot;</span>
      <span class="string">&quot;(right-click) menu.&lt;/p&gt;&quot;</span>

  <span class="preprocessor">#ifdef Q_OS_MAC</span>
      <span class="string">&quot;&lt;p&gt;On OS X, the \&quot;Black\&quot; dock widget has been created as a &quot;</span>
      <span class="string">&quot;&lt;em&gt;Drawer&lt;/em&gt;, which is a special kind of QDockWidget.&lt;/p&gt;&quot;</span>
  <span class="preprocessor">#endif</span>
      ;

  Q_DECLARE_METATYPE(<span class="type"><a href="qdockwidget.html">QDockWidget</a></span><span class="operator">::</span>DockWidgetFeatures)

  MainWindow<span class="operator">::</span>MainWindow(<span class="keyword">const</span> CustomSizeHintMap <span class="operator">&amp;</span>customSizeHints<span class="operator">,</span>
                         <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowFlags flags)
      : <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>(parent<span class="operator">,</span> flags)
  {
      Q_UNUSED(message);
      setObjectName(<span class="string">&quot;MainWindow&quot;</span>);
      setWindowTitle(<span class="string">&quot;Qt Main Window Example&quot;</span>);

      <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>center <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>(<span class="keyword">this</span>);
      center<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
      center<span class="operator">-</span><span class="operator">&gt;</span>setMinimumSize(<span class="number">400</span><span class="operator">,</span> <span class="number">205</span>);
      setCentralWidget(center);

      setupToolBar();
      setupMenuBar();
      setupDockWidgets(customSizeHints);

      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(tr(<span class="string">&quot;Status Bar&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>actionTriggered(<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action)
  {
      <a href="../qtcore/qtglobal.html#qDebug">qDebug</a>(<span class="string">&quot;action '%s' triggered&quot;</span><span class="operator">,</span> action<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toLocal8Bit()<span class="operator">.</span>data());
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setupToolBar()
  {
  <span class="preprocessor">#ifdef Q_OS_OSX</span>
      setUnifiedTitleAndToolBarOnMac(<span class="keyword">true</span>);
  <span class="preprocessor">#endif</span>

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">3</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          ToolBar <span class="operator">*</span>tb <span class="operator">=</span> <span class="keyword">new</span> ToolBar(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(<span class="string">&quot;Tool Bar %1&quot;</span>)<span class="operator">.</span>arg(i <span class="operator">+</span> <span class="number">1</span>)<span class="operator">,</span> <span class="keyword">this</span>);
          toolBars<span class="operator">.</span>append(tb);
          addToolBar(tb);
      }
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setupMenuBar()
  {
      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>menu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));

      menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Save layout...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>saveLayout);
      menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Load layout...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>loadLayout);
      menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Switch layout direction&quot;</span>)<span class="operator">,</span><span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>switchLayoutDirection);

      menu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Quit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close);

      mainWindowMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Main window&quot;</span>));

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Animated docks&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> AnimatedDocks);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Allow nested docks&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> AllowNestedDocks);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Allow tabbed docks&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> AllowTabbedDocks);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Force tabbed docks&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> ForceTabbedDocks);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Vertical tabs&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> VerticalTabs);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      action <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Grouped dragging&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(dockOptions() <span class="operator">&amp;</span> GroupedDragging);
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setDockOptions);

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>toolBarMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Tool bars&quot;</span>));
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> toolBars<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i)
          toolBarMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(toolBars<span class="operator">.</span>at(i)<span class="operator">-</span><span class="operator">&gt;</span>toolbarMenu());

  <span class="preprocessor">#ifdef Q_OS_OSX</span>
      toolBarMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      action <span class="operator">=</span> toolBarMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Unified&quot;</span>));
      action<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      action<span class="operator">-</span><span class="operator">&gt;</span>setChecked(unifiedTitleAndToolBarOnMac());
      connect(action<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>toggled<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qmainwindow.html">QMainWindow</a></span><span class="operator">::</span>setUnifiedTitleAndToolBarOnMac);
  <span class="preprocessor">#endif</span>

      dockWidgetMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Dock Widgets&quot;</span>));

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>aboutMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;About&quot;</span>));
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>aboutAct <span class="operator">=</span> aboutMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;About&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>about);
      aboutAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Show the application's About box&quot;</span>));

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>aboutQtAct <span class="operator">=</span> aboutMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;About &amp;Qt&quot;</span>)<span class="operator">,</span> <a href="qapplication.html#qApp">qApp</a><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>aboutQt);
      aboutQtAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Show the Qt library's About box&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setDockOptions()
  {
      DockOptions opts;
      <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">*</span><span class="operator">&gt;</span> actions <span class="operator">=</span> mainWindowMenu<span class="operator">-</span><span class="operator">&gt;</span>actions();

      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> AnimatedDocks;
      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">1</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> AllowNestedDocks;
      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">2</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> AllowTabbedDocks;
      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">3</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> ForceTabbedDocks;
      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">4</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> VerticalTabs;
      <span class="keyword">if</span> (actions<span class="operator">.</span>at(<span class="number">5</span>)<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          opts <span class="operator">|</span><span class="operator">=</span> GroupedDragging;

      <span class="type"><a href="qmainwindow.html">QMainWindow</a></span><span class="operator">::</span>setDockOptions(opts);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>saveLayout()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName
          <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Save layout&quot;</span>));
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;
      <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(fileName);
      <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>WriteOnly)) {
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> msg <span class="operator">=</span> tr(<span class="string">&quot;Failed to open %1\n%2&quot;</span>)
                          <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)<span class="operator">,</span> file<span class="operator">.</span>errorString());
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span> msg);
          <span class="keyword">return</span>;
      }

      <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> geo_data <span class="operator">=</span> saveGeometry();
      <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> layout_data <span class="operator">=</span> saveState();

      bool ok <span class="operator">=</span> file<span class="operator">.</span>putChar((<span class="type"><a href="../qtcore/qtglobal.html#uchar-typedef">uchar</a></span>)geo_data<span class="operator">.</span>size());
      <span class="keyword">if</span> (ok)
          ok <span class="operator">=</span> file<span class="operator">.</span>write(geo_data) <span class="operator">=</span><span class="operator">=</span> geo_data<span class="operator">.</span>size();
      <span class="keyword">if</span> (ok)
          ok <span class="operator">=</span> file<span class="operator">.</span>write(layout_data) <span class="operator">=</span><span class="operator">=</span> layout_data<span class="operator">.</span>size();

      <span class="keyword">if</span> (<span class="operator">!</span>ok) {
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> msg <span class="operator">=</span> tr(<span class="string">&quot;Error writing to %1\n%2&quot;</span>)
                          <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)<span class="operator">,</span> file<span class="operator">.</span>errorString());
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span> msg);
          <span class="keyword">return</span>;
      }
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>loadLayout()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName
          <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getOpenFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Load layout&quot;</span>));
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;
      <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(fileName);
      <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly)) {
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> msg <span class="operator">=</span> tr(<span class="string">&quot;Failed to open %1\n%2&quot;</span>)
                          <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)<span class="operator">,</span> file<span class="operator">.</span>errorString());
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span> msg);
          <span class="keyword">return</span>;
      }

      <span class="type"><a href="../qtcore/qtglobal.html#uchar-typedef">uchar</a></span> geo_size;
      <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> geo_data;
      <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> layout_data;

      bool ok <span class="operator">=</span> file<span class="operator">.</span>getChar((<span class="type">char</span><span class="operator">*</span>)<span class="operator">&amp;</span>geo_size);
      <span class="keyword">if</span> (ok) {
          geo_data <span class="operator">=</span> file<span class="operator">.</span>read(geo_size);
          ok <span class="operator">=</span> geo_data<span class="operator">.</span>size() <span class="operator">=</span><span class="operator">=</span> geo_size;
      }
      <span class="keyword">if</span> (ok) {
          layout_data <span class="operator">=</span> file<span class="operator">.</span>readAll();
          ok <span class="operator">=</span> layout_data<span class="operator">.</span>size() <span class="operator">&gt;</span> <span class="number">0</span>;
      }

      <span class="keyword">if</span> (ok)
          ok <span class="operator">=</span> restoreGeometry(geo_data);
      <span class="keyword">if</span> (ok)
          ok <span class="operator">=</span> restoreState(layout_data);

      <span class="keyword">if</span> (<span class="operator">!</span>ok) {
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> msg <span class="operator">=</span> tr(<span class="string">&quot;Error reading %1&quot;</span>)<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName));
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span> msg);
          <span class="keyword">return</span>;
      }
  }

  <span class="keyword">static</span> <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>addCornerAction(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text<span class="operator">,</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span> <span class="operator">*</span>mw<span class="operator">,</span> <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>menu<span class="operator">,</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span> <span class="operator">*</span>group<span class="operator">,</span>
                                  <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Corner c<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DockWidgetArea a)
  {
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>result <span class="operator">=</span> menu<span class="operator">-</span><span class="operator">&gt;</span>addAction(text<span class="operator">,</span> mw<span class="operator">,</span> <span class="operator">[</span><span class="operator">=</span><span class="operator">]</span>() { mw<span class="operator">-</span><span class="operator">&gt;</span>setCorner(c<span class="operator">,</span> a); });
      result<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      group<span class="operator">-</span><span class="operator">&gt;</span>addAction(result);
      <span class="keyword">return</span> result;
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setupDockWidgets(<span class="keyword">const</span> CustomSizeHintMap <span class="operator">&amp;</span>customSizeHints)
  {
      <a href="../qtcore/qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a><span class="operator">&lt;</span><span class="type"><a href="qdockwidget.html">QDockWidget</a></span><span class="operator">::</span>DockWidgetFeatures<span class="operator">&gt;</span>();

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>cornerMenu <span class="operator">=</span> dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Top left corner&quot;</span>));
      <span class="type"><a href="qactiongroup.html">QActionGroup</a></span> <span class="operator">*</span>group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);
      group<span class="operator">-</span><span class="operator">&gt;</span>setExclusive(<span class="keyword">true</span>);
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>cornerAction <span class="operator">=</span> addCornerAction(tr(<span class="string">&quot;Top dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopLeftCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea);
      cornerAction<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
      addCornerAction(tr(<span class="string">&quot;Left dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopLeftCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea);

      cornerMenu <span class="operator">=</span> dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Top right corner&quot;</span>));
      group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);
      group<span class="operator">-</span><span class="operator">&gt;</span>setExclusive(<span class="keyword">true</span>);
      cornerAction <span class="operator">=</span> addCornerAction(tr(<span class="string">&quot;Top dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopRightCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea);
      cornerAction<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
      addCornerAction(tr(<span class="string">&quot;Right dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopRightCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea);

      cornerMenu <span class="operator">=</span> dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Bottom left corner&quot;</span>));
      group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);
      group<span class="operator">-</span><span class="operator">&gt;</span>setExclusive(<span class="keyword">true</span>);
      cornerAction <span class="operator">=</span> addCornerAction(tr(<span class="string">&quot;Bottom dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomLeftCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea);
      cornerAction<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
      addCornerAction(tr(<span class="string">&quot;Left dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomLeftCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea);

      cornerMenu <span class="operator">=</span> dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;Bottom right corner&quot;</span>));
      group <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);
      group<span class="operator">-</span><span class="operator">&gt;</span>setExclusive(<span class="keyword">true</span>);
      cornerAction <span class="operator">=</span> addCornerAction(tr(<span class="string">&quot;Bottom dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomRightCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea);
      cornerAction<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
      addCornerAction(tr(<span class="string">&quot;Right dock area&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> cornerMenu<span class="operator">,</span> group<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomRightCorner<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea);

      dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct</span> Set {
          <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span> name;
          <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span> flags;
          <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DockWidgetArea area;
      } sets <span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
  <span class="preprocessor">#ifndef Q_OS_MAC</span>
          { <span class="string">&quot;Black&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea }<span class="operator">,</span>
  <span class="preprocessor">#else</span>
          { <span class="string">&quot;Black&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Drawer<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea }<span class="operator">,</span>
  <span class="preprocessor">#endif</span>
          { <span class="string">&quot;White&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea }<span class="operator">,</span>
          { <span class="string">&quot;Red&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea }<span class="operator">,</span>
          { <span class="string">&quot;Green&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea }<span class="operator">,</span>
          { <span class="string">&quot;Blue&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea }<span class="operator">,</span>
          { <span class="string">&quot;Yellow&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea }
      };
      <span class="keyword">const</span> <span class="type">int</span> setCount <span class="operator">=</span> <span class="keyword">sizeof</span>(sets) <span class="operator">/</span> <span class="keyword">sizeof</span>(Set);

      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qicon.html">QIcon</a></span> qtIcon(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span>(<span class="string">&quot;:/res/qt.png&quot;</span>));
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> setCount; <span class="operator">+</span><span class="operator">+</span>i) {
          ColorSwatch <span class="operator">*</span>swatch <span class="operator">=</span> <span class="keyword">new</span> ColorSwatch(tr(sets<span class="operator">[</span>i<span class="operator">]</span><span class="operator">.</span>name)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowFlags(sets<span class="operator">[</span>i<span class="operator">]</span><span class="operator">.</span>flags));
          <span class="keyword">if</span> (i <span class="operator">%</span> <span class="number">2</span>)
              swatch<span class="operator">-</span><span class="operator">&gt;</span>setWindowIcon(qtIcon);
          <span class="keyword">if</span> (qstrcmp(sets<span class="operator">[</span>i<span class="operator">]</span><span class="operator">.</span>name<span class="operator">,</span> <span class="string">&quot;Blue&quot;</span>) <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
              BlueTitleBar <span class="operator">*</span>titlebar <span class="operator">=</span> <span class="keyword">new</span> BlueTitleBar(swatch);
              swatch<span class="operator">-</span><span class="operator">&gt;</span>setTitleBarWidget(titlebar);
              connect(swatch<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdockwidget.html">QDockWidget</a></span><span class="operator">::</span>topLevelChanged<span class="operator">,</span> titlebar<span class="operator">,</span> <span class="operator">&amp;</span>BlueTitleBar<span class="operator">::</span>updateMask);
              connect(swatch<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdockwidget.html">QDockWidget</a></span><span class="operator">::</span>featuresChanged<span class="operator">,</span> titlebar<span class="operator">,</span> <span class="operator">&amp;</span>BlueTitleBar<span class="operator">::</span>updateMask<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>QueuedConnection);
          }

          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name <span class="operator">=</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(sets<span class="operator">[</span>i<span class="operator">]</span><span class="operator">.</span>name);
          <span class="keyword">if</span> (customSizeHints<span class="operator">.</span>contains(name))
              swatch<span class="operator">-</span><span class="operator">&gt;</span>setCustomSizeHint(customSizeHints<span class="operator">.</span>value(name));

          addDockWidget(sets<span class="operator">[</span>i<span class="operator">]</span><span class="operator">.</span>area<span class="operator">,</span> swatch);
          dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(swatch<span class="operator">-</span><span class="operator">&gt;</span>colorSwatchMenu());
      }

      destroyDockWidgetMenu <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qmenu.html">QMenu</a></span>(tr(<span class="string">&quot;Destroy dock widget&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      connect(destroyDockWidgetMenu<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qmenu.html">QMenu</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>destroyDockWidget);

      dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Add dock widget...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>createDockWidget);
      dockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(destroyDockWidgetMenu);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>switchLayoutDirection()
  {
      <span class="keyword">if</span> (layoutDirection() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftToRight)
          <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>setLayoutDirection(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightToLeft);
      <span class="keyword">else</span>
          <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>setLayoutDirection(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftToRight);
  }

  <span class="keyword">class</span> CreateDockWidgetDialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span>
  {
  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> CreateDockWidgetDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> enteredObjectName() <span class="keyword">const</span> { <span class="keyword">return</span> m_objectName<span class="operator">-</span><span class="operator">&gt;</span>text(); }
      <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DockWidgetArea location() <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>m_objectName;
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>m_location;
  };

  CreateDockWidgetDialog<span class="operator">::</span>CreateDockWidgetDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qdialog.html">QDialog</a></span>(parent)
      <span class="operator">,</span> m_objectName(<span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="keyword">this</span>))
      <span class="operator">,</span> m_location(<span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>(<span class="keyword">this</span>))
  {
      setWindowTitle(tr(<span class="string">&quot;Add Dock Widget&quot;</span>));
      setWindowFlags(windowFlags() <span class="operator">&amp;</span> <span class="operator">~</span><span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowContextHelpButtonHint);
      <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>(<span class="keyword">this</span>);

      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Object name:&quot;</span>))<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(m_objectName<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);

      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Location:&quot;</span>))<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>);
      m_location<span class="operator">-</span><span class="operator">&gt;</span>setEditable(<span class="keyword">false</span>);
      m_location<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Top&quot;</span>));
      m_location<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Left&quot;</span>));
      m_location<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Right&quot;</span>));
      m_location<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Bottom&quot;</span>));
      m_location<span class="operator">-</span><span class="operator">&gt;</span>addItem(tr(<span class="string">&quot;Restore&quot;</span>));
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(m_location<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);

      <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span> <span class="operator">*</span>buttonBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span>(<span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>Ok <span class="operator">|</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>Cancel<span class="operator">,</span> <span class="keyword">this</span>);
      connect(buttonBox<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>rejected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>reject);
      connect(buttonBox<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>accepted<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>accept);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(buttonBox<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
  }

  <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DockWidgetArea CreateDockWidgetDialog<span class="operator">::</span>location() <span class="keyword">const</span>
  {
      <span class="keyword">switch</span> (m_location<span class="operator">-</span><span class="operator">&gt;</span>currentIndex()) {
          <span class="keyword">case</span> <span class="number">0</span>: <span class="keyword">return</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea;
          <span class="keyword">case</span> <span class="number">1</span>: <span class="keyword">return</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea;
          <span class="keyword">case</span> <span class="number">2</span>: <span class="keyword">return</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea;
          <span class="keyword">case</span> <span class="number">3</span>: <span class="keyword">return</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea;
          <span class="keyword">default</span>:
              <span class="keyword">break</span>;
      }
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoDockWidgetArea;
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createDockWidget()
  {
      CreateDockWidgetDialog dialog(<span class="keyword">this</span>);
      <span class="keyword">if</span> (dialog<span class="operator">.</span>exec() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Rejected)
          <span class="keyword">return</span>;

      <span class="type"><a href="qdockwidget.html">QDockWidget</a></span> <span class="operator">*</span>dw <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdockwidget.html">QDockWidget</a></span>;
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name <span class="operator">=</span> dialog<span class="operator">.</span>enteredObjectName();
      dw<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(name);
      dw<span class="operator">-</span><span class="operator">&gt;</span>setWindowTitle(name);
      dw<span class="operator">-</span><span class="operator">&gt;</span>setWidget(<span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>);

      <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DockWidgetArea area <span class="operator">=</span> dialog<span class="operator">.</span>location();
      <span class="keyword">switch</span> (area) {
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea:
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea:
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TopDockWidgetArea:
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>BottomDockWidgetArea:
              addDockWidget(area<span class="operator">,</span> dw);
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              <span class="keyword">if</span> (<span class="operator">!</span>restoreDockWidget(dw)) {
                  <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>()<span class="operator">,</span> tr(<span class="string">&quot;Failed to restore dock widget&quot;</span>));
                  <span class="keyword">delete</span> dw;
                  <span class="keyword">return</span>;
              }
              <span class="keyword">break</span>;
      }

      extraDockWidgets<span class="operator">.</span>append(dw);
      destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
      destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(<span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(name<span class="operator">,</span> <span class="keyword">this</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>destroyDockWidget(<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action)
  {
      <span class="type">int</span> index <span class="operator">=</span> destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>actions()<span class="operator">.</span>indexOf(action);
      <span class="keyword">delete</span> extraDockWidgets<span class="operator">.</span>takeAt(index);
      destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>removeAction(action);
      action<span class="operator">-</span><span class="operator">&gt;</span>deleteLater();

      <span class="keyword">if</span> (destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>isEmpty())
          destroyDockWidgetMenu<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>about()
  {
      <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;About MainWindows&quot;</span>)<span class="operator">,</span> message);
  }

</pre>
</div>
<!-- @@@mainwindows/mainwindow/mainwindow.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
